package com.dawn.usermanage.action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import weibo4j.Weibo;

import com.dawn.sinaoption.common.SinaCommon;
import com.dawn.usermanage.bean.SinaInfoBean;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.service.SinaUserMService;
import com.dawn.usermanage.service.UserManageService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("registerAction")
@Scope("prototype")
public class RegisterAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 4896220852536522687L;
	private static Logger logger = Logger.getLogger(RegisterAction.class);
	private UserBean userBean;
	private SinaInfoBean sinaUserInfo;

	@Autowired
	private SinaUserMService sinaUserMService;

	@Autowired
	private UserManageService userManageService;;

	private Map<String, Object> session;

	public String showSInfoAndReg() {

		String sinaId = session.get("sinaId").toString();

		sinaUserInfo = sinaUserMService.findSUserInfo(sinaId);
		if (sinaUserInfo == null) {
			Weibo weibo = SinaCommon.getWeibo(session);
			sinaUserInfo = sinaUserMService.addSinaBasic(sinaId, weibo);
		}
		return sinaUserInfo == null ? ERROR : SUCCESS;
	}

	public String register() {
		String sinaId = userBean.getSinaInfoBean().getSinaId();
		SinaInfoBean sinaInfoBean = sinaUserMService.findSUserInfo(sinaId);
		if (userManageService.findUserBySinaId(sinaId) != null)
			return SUCCESS;
		userBean.setSinaInfoBean(sinaInfoBean);
		userBean.setCreditLevel(10);
		userBean.setRemainNumByDay(30);
		return sinaUserMService.addUserInfo(userBean) ? SUCCESS : ERROR;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public SinaInfoBean getSinaUserInfo() {
		return sinaUserInfo;
	}

	public void setSinaUserInfo(SinaInfoBean sinaUserInfo) {
		this.sinaUserInfo = sinaUserInfo;
	}

}
